package com.hxd.otc.module.convenience.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseAdapter;
import com.hxd.otc.module.convenience.bean.AppStoreBean;
import com.hxd.otc.utils.GlideUtils;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by lzy on 2019/1/3 0003.
 * description ${应用市场列表Adapter}
 */
public class AppStoreAdapter extends BaseAdapter<AppStoreAdapter.AppStoreHolder, AppStoreBean> {
    private OnDownloadListener mOnDownloadListener;

    public AppStoreAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindHolder(@NonNull AppStoreHolder holder, int position) {
        final AppStoreBean item = getItem(position);
        holder.tvAppName.setText(item.getAppName());
        holder.tvAppDesc.setText(item.getDesci());
        GlideUtils.loadNormal(mContext, getServiceImageUrlByPath(item.getAppLogo()), holder.ivLogo);
        holder.tvDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnDownloadListener == null) return;
                mOnDownloadListener.onDownload(item);
            }
        });
    }

    /**
     * TODO 临时使用
     *
     * @param path
     */
    public String getServiceImageUrlByPath(String path) {
        return "https://wotcpdf.oss-ap-southeast-1.aliyuncs.com/" + path;
    }

    @NonNull
    @Override
    public AppStoreHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AppStoreHolder(mInflater.inflate(R.layout.item_app_store, parent, false));
    }

    static class AppStoreHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_logo)
        ImageView ivLogo;
        @BindView(R.id.tv_app_name)
        TextView tvAppName;
        @BindView(R.id.tv_app_desc)
        TextView tvAppDesc;
        @BindView(R.id.tv_download)
        TextView tvDownload;

        AppStoreHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public void setOnDownloadListener(OnDownloadListener onDownloadListener) {
        mOnDownloadListener = onDownloadListener;
    }

    public interface OnDownloadListener {
        void onDownload(AppStoreBean appStoreBean);
    }
}
